/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aplikasirawatinap;

import Service.Koneksi;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author Saptaji
 */
public class DataPasien extends javax.swing.JFrame {

    /**
     * Creates new form DataPasien
     */
    private Integer row;
    public Connection conn;
    PreparedStatement psmt;
    String select="Select *From pasien ORDER BY id_pasien ASC";
    private JTable jTabel = new JTable();
    private DefaultTableModel DfltTblMode;
    private TableColumn column;
    
    public DataPasien() {
        initComponents();
        tampilDataKeTabel();
    }
    
    private void tampilDataKeTabel() {
        jTabel = jTable1;
        tampilkandata(jTabel);
    }
    
    private void tampilDataKeTabelcari() {
        jTabel = jTable1;
        cari(jTabel);
    }
    
    private void tampilkandata(JTable jTabel) {
     try {
            Object[] field = {"No","id_pasien","tgl_masuk","nm_depan","nm_belakang","gender","tgl_lahir","nm_ayah","agama","alamat","pekerjaan","umur","gol_darah","status","nm_ruangan","kel_ruangan","lokasi","nm_dokter","diagnosa","DP"};
            DfltTblMode = new DefaultTableModel(null, field);
            jTabel.setModel(DfltTblMode);
           
            conn=Koneksi.getConnection();
            
            PreparedStatement statement = conn.prepareStatement(select);
            ResultSet rs =statement.executeQuery(select);
                  
            int no = 0;
            while (rs.next()) {
                no++;
                String kolom1 = String.valueOf(no).toString();
                String kolom2 = rs.getString("id_pasien");
                String kolom3 = rs.getString("tanggal_masuk");
                String kolom4 = rs.getString("first_name");
                String kolom5 = rs.getString("last_name");
                String kolom6 = rs.getString("gender");
                String kolom7 = rs.getString("tgl_lahir");
                String kolom8 = rs.getString("nm_ayah_kandung");
                String kolom9 = rs.getString("agama");
                String kolom10 = rs.getString("alamat");
                String kolom11 = rs.getString("Pekerjaan");
                String kolom12 = rs.getString("umur");
                String kolom13 = rs.getString("gol_darah");
                String kolom14 = rs.getString("status");
                String kolom15 = rs.getString("nm_ruangan");
                String kolom16 = rs.getString("kel_ruangan");
                String kolom17 = rs.getString("lokasi");
                String kolom18 = rs.getString("nm_dokter");
                String kolom19 = rs.getString("diagnosa");
                String kolom20 = rs.getString("DP");
                String[] data = {kolom1, kolom2, kolom3, kolom4, kolom5, kolom6, kolom7, kolom8, kolom9, kolom10, kolom11, kolom12, kolom13, kolom14, kolom15, kolom16, kolom17, kolom18, kolom19, kolom20};
                DfltTblMode.addRow(data);
            }
            
            jTabel.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
            column = jTabel.getColumnModel().getColumn(0);
            column.setPreferredWidth(40);
            column = jTabel.getColumnModel().getColumn(1);
            column.setPreferredWidth(100);
            column = jTabel.getColumnModel().getColumn(2);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(3);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(4);
            column.setPreferredWidth(150); 
            column = jTabel.getColumnModel().getColumn(5);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(6);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(7);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(8);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(9);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(10);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(11);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(12);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(13);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(14);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(15);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(16);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(17);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(18);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(19);
            column.setPreferredWidth(150);
          }
          catch (SQLException e) {
              JOptionPane.showMessageDialog(this, "Koneksi gagal: " +e);
          }  
   }
    
   public void cari(JTable jTabel){
    try {
            Object[] field = {"No","id_pasien","tgl_masuk","nm_depan","nm_belakang","gender","tgl_lahir","nm_ayah","agama","alamat","pekerjaan","umur","gol_darah","status","nm_ruangan","kel_ruangan","lokasi","nm_dokter","diagnosa","DP"};
            DfltTblMode = new DefaultTableModel(null, field);
            jTabel.setModel(DfltTblMode);
           
            conn=Koneksi.getConnection();
            
            String search ="Select * from pasien where tanggal_masuk like '%" + tx_search.getText() + "%'" +
                         "or first_name like '%" + tx_search.getText() + "%'";
            PreparedStatement statement = conn.prepareStatement(search);
            ResultSet rs =statement.executeQuery(search);
                  
            int no = 0;
            while (rs.next()) {
                no++;
                String kolom1 = String.valueOf(no).toString();
                String kolom2 = rs.getString("id_pasien");
                String kolom3 = rs.getString("tanggal_masuk");
                String kolom4 = rs.getString("first_name");
                String kolom5 = rs.getString("last_name");
                String kolom6 = rs.getString("gender");
                String kolom7 = rs.getString("tgl_lahir");
                String kolom8 = rs.getString("nm_ayah_kandung");
                String kolom9 = rs.getString("agama");
                String kolom10 = rs.getString("alamat");
                String kolom11 = rs.getString("Pekerjaan");
                String kolom12 = rs.getString("umur");
                String kolom13 = rs.getString("gol_darah");
                String kolom14 = rs.getString("status");
                String kolom15 = rs.getString("nm_ruangan");
                String kolom16 = rs.getString("kel_ruangan");
                String kolom17 = rs.getString("lokasi");
                String kolom18 = rs.getString("nm_dokter");
                String kolom19 = rs.getString("diagnosa");
                String kolom20 = rs.getString("DP");
                String[] data = {kolom1, kolom2, kolom3, kolom4, kolom5, kolom6, kolom7, kolom8, kolom9, kolom10, kolom11, kolom12, kolom13, kolom14, kolom15, kolom16, kolom17, kolom18, kolom19, kolom20};
                DfltTblMode.addRow(data);
            }
            
            jTabel.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
            column = jTabel.getColumnModel().getColumn(0);
            column.setPreferredWidth(40);
            column = jTabel.getColumnModel().getColumn(1);
            column.setPreferredWidth(100);
            column = jTabel.getColumnModel().getColumn(2);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(3);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(4);
            column.setPreferredWidth(150); 
            column = jTabel.getColumnModel().getColumn(5);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(6);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(7);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(8);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(9);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(10);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(11);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(12);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(13);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(14);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(15);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(16);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(17);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(18);
            column.setPreferredWidth(150);
            column = jTabel.getColumnModel().getColumn(19);
            column.setPreferredWidth(150);
          }
          catch (SQLException e) {
              JOptionPane.showMessageDialog(this, "Koneksi gagal: " +e);
          }  
   }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        tx_search = new javax.swing.JTextField();
        b_search = new javax.swing.JButton();
        b_view = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMnKembali = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("DAFTAR PASIEN");

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel11.setText("Search berdasarkan tgl.masuk/nama depan");

        b_search.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/search.png"))); // NOI18N
        b_search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_searchActionPerformed(evt);
            }
        });

        b_view.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/view_user.png"))); // NOI18N
        b_view.setText("VIEW ALL DATA PASIEN");
        b_view.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_viewActionPerformed(evt);
            }
        });

        jMenuBar1.setBackground(new java.awt.Color(236, 236, 236));

        jMnKembali.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/back.png"))); // NOI18N
        jMnKembali.setToolTipText("Menu Utama");
        jMnKembali.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jMnKembaliMousePressed(evt);
            }
        });
        jMenuBar1.add(jMnKembali);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(362, 362, 362)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(b_view)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tx_search, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(b_search))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 746, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(54, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jLabel1)
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel11)
                        .addComponent(tx_search, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(b_search))
                .addGap(28, 28, 28)
                .addComponent(b_view)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(94, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMnKembaliMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMnKembaliMousePressed
        // TODO add your handling code here:
        new UserForm().setVisible(true);
        dispose();
    }//GEN-LAST:event_jMnKembaliMousePressed

    private void b_searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_searchActionPerformed
        // TODO add your handling code here:
        tampilDataKeTabelcari();
    }//GEN-LAST:event_b_searchActionPerformed

    private void b_viewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_viewActionPerformed
        // TODO add your handling code here:
        tampilDataKeTabel();
    }//GEN-LAST:event_b_viewActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        new UserForm().setVisible(true);
        dispose();
    }//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DataPasien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DataPasien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DataPasien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DataPasien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DataPasien().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton b_search;
    private javax.swing.JButton b_view;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu jMnKembali;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField tx_search;
    // End of variables declaration//GEN-END:variables
}
